#!/bin/sh
#
# $Id: proxy.sh 1198 2007-12-22 11:43:15Z drake.diedrich $
#
# Set up the local machine (Debian or derivative) with the pass-through proxy.

set -e

VIRTNET="192.168.23.0/24"
VIRTHOST="192.168.23.1"
[ -x /etc/default/testenv ] && . /etc/default/testenv

# if apache2 was already installed and the default VirtualHost is already
# enabled, leave it alone.  Else install and disable the default VirtualHost
if [ ! -f /etc/apache2/sites-enabled/000-default ] ; then
  apt-get install -y apache2
  rm -f /etc/apache2/sites-enabled/000-default
else
  apt-get install -y apache2
fi

cat <<EOF >/etc/apache2/sites-available/proxy
#
# generated by \$Id: proxy.sh 1198 2007-12-22 11:43:15Z drake.diedrich $
#
# An apache2 virtualhost configuration to do pass-through proxying to
# the upstream Ubuntu and Debian archives.  This makes the host machine
# look like an actual archive server to the client instances, and avoids
# the need to explicitly declare an http_proxy to the installer.
# apt-get and d-i have issues with pipelining to squid caches - proxying
# this way avoids those issues.

NameVirtualHost $VIRTHOST
<VirtualHost $VIRTHOST>
  ServerName www.localnet
  ProxyRequests Off
  ProxyPreserveHost Off

  CacheRoot "/var/www/proxy"
  CacheSize 1024000
  CacheMaxExpire 24
  CacheLastModifiedFactor 0.1
  CacheDefaultExpire 1


  <Proxy *>
    Order deny,allow
    Allow from $VIRTNET
  </Proxy>

  <Directory /debian/>
    ProxyPass http://ftp.us.debian.org/debian/
    ProxyPassReverse http://ftp.us.debian.org/debian/
    Allow from $VIRTNET
  </Directory>

  <Directory /debian-security/>
    ProxyPass http://security.debian.org/debian-security/
    ProxyPassReverse http://security.debian.org/debian-security/
    Allow from $VIRTNET
  </Directory>

  <Directory /ubuntu/>
    ProxyPass http://archive.ubuntu.com/ubuntu/
    ProxyPassReverse http://archive.ubuntu.com/ubuntu/
    Allow from $VIRTNET
  </Directory>

  LogLevel warn
  CustomLog /var/log/apache2/proxy.log combined

</VirtualHost>
EOF

ln -sf /etc/apache2/sites-available/proxy /etc/apache2/sites-enabled/
ln -sf /etc/apache2/mods-available/proxy.conf /etc/apache2/mods-enabled/
ln -sf /etc/apache2/mods-available/proxy.load /etc/apache2/mods-enabled/
ln -sf /etc/apache2/mods-available/proxy_http.load /etc/apache2/mods-enabled/
ln -sf /etc/apache2/mods-available/cache.load /etc/apache2/mods-enabled/
ln -sf /etc/apache2/mods-available/disk_cache.load /etc/apache2/mods-enabled/

if [ ! -d /var/www/proxy ] ; then
  mkdir /var/www/proxy
  chown www-data:www-data /var/www/proxy
fi

/etc/init.d/apache2 restart
